﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using QuickDev.SSO;
using QuickDev.Common.JS;

namespace QuickDev.Web.App
{
    public partial class SSO : System.Web.UI.Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load( object sender, EventArgs e )
        {
            if ( SsoEngine.IsAuthenticated )
            {
                SsoTicket ticket = SsoEngine.GetAuthenticatedTicket();

                //获得 认证门票
                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1, ticket.UserId + "," + ticket.UserName, DateTime.Now, DateTime.Now.AddDays( 1 ), true, ticket.Roles );

                //加密 认证门票，并保存到 HttpCookie 中
                string encryptedTicket = FormsAuthentication.Encrypt( authTicket );
                HttpCookie userCookies = new HttpCookie( FormsAuthentication.FormsCookieName, encryptedTicket );
                HttpContext.Current.Response.Cookies.Add( userCookies );

                //从用户自定义的数据中获取用户的角色
                string[] roles = ticket.Roles.Length > 0 ? ticket.Roles.Split( new char[] { ',' } ) : null;

                #region 获得用户角色,并跳转至相应的页面


                foreach ( string role in roles )
                {
                    if ( role == "Administrator" )
                    {
                        Response.Redirect( "~/Administration/Default.aspx" );
                        break;
                    }
                }

                foreach ( string role in roles )
                {
                    if ( role == "Sales" )
                    {
                        Response.Redirect( String.Format( "~/{0}/Default.aspx", role ) );
                        break;
                    }
                }

                foreach ( string role in roles )
                {
                    if ( role == "Suppliers" )
                    {
                        Response.Redirect( String.Format( "~/{0}/Default.aspx", role ) );
                        break;
                    }
                }

                foreach ( string role in roles )
                {
                    if ( role == "Vendors" )
                    {
                        Response.Redirect( String.Format( "~/{0}/Default.aspx", role ) );
                        break;
                    }
                }

                JavaScript.MsgBoxAlarm( "No Permission ! ", this.Page );

                #endregion
            }
            else if ( SsoEngine.IsAuthenticating )
            {
                ResponseToken token;
                if ( !SsoEngine.Authenticate( out token ) )
                {
                    JavaScript.MsgBoxAlarm( "Error Authentication ! ", this.Page );
                }
                else
                {
                    //获得 认证门票
                    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1, token.UserId + "," + token.UserName, DateTime.Now, DateTime.Now.AddDays( 1 ), true, token.Roles );

                    //加密 认证门票，并保存到 HttpCookie 中
                    string encryptedTicket = FormsAuthentication.Encrypt( authTicket );
                    HttpCookie userCookies = new HttpCookie( FormsAuthentication.FormsCookieName, encryptedTicket );
                    HttpContext.Current.Response.Cookies.Add( userCookies );

                    //从用户自定义的数据中获取用户的角色
                    string[] roles = token.Roles.Split( new char[] { ',' } );

                    #region 获得用户角色,并跳转至相应的页面

                    foreach ( string role in roles )
                    {
                        if ( role == "Administrator" )
                        {
                            Response.Redirect( "~/Administration/Default.aspx" );
                            break;
                        }
                    }

                    foreach ( string role in roles )
                    {
                        if ( role == "Sales" )
                        {
                            Response.Redirect( String.Format( "~/{0}/Default.aspx", role ) );
                            break;
                        }
                    }

                    foreach ( string role in roles )
                    {
                        if ( role == "Suppliers" )
                        {
                            Response.Redirect( String.Format( "~/{0}/Default.aspx", role ) );
                            break;
                        }
                    }

                    JavaScript.MsgBoxAlarm( "No Permission ! ", this.Page );

                    #endregion
                }
            }
            else
            {
                SsoEngine.RedirectToLogon();
            }
        }
    }
}
